package com.whut.monitor.service.impl;

import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.Tree;
import com.whut.monitor.constant.EquipmentCategory;
import com.whut.monitor.constant.EquipmentType;
import com.whut.monitor.dao.IBaseDao;
import com.whut.monitor.model.EquipmentEntity;
import com.whut.monitor.service.IEquipmentService;
import com.whut.monitor.vo.EquipmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @author Sandeepin
 * 设备服务层实现
 */
@Service
public class EquipmentServiceImpl implements IEquipmentService {

	@Autowired
	private IBaseDao<EquipmentEntity> equipmentDao;


	@Override
	public void add(EquipmentVo equipmentVo) {
		EquipmentEntity entity = new EquipmentEntity();
		entity.setEquipmentCategory(equipmentVo.getEquipmentCategory());
		entity.setEquipmentType(equipmentVo.getEquipmentType());
		entity.setEquipmentNum(equipmentVo.getEquipmentNum());
		entity.setEquipmentName(equipmentVo.getEquipmentName());
		entity.setCompany(equipmentVo.getCompany());
		entity.setProductDate(equipmentVo.getProductDate());
		equipmentDao.save(entity);
	}
	
	@Override
	public void delete(Integer id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		equipmentDao.executeHql("delete from EquipmentEntity t where t.id=:id",params);
	}

	@Override
	public void edit(EquipmentVo equipmentVo) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("equipmentName", equipmentVo.getEquipmentName());
		params.put("equipmentNum", equipmentVo.getEquipmentNum());
		params.put("equipmentCategory", equipmentVo.getEquipmentCategory());
		params.put("equipmentType", equipmentVo.getEquipmentType());
		params.put("productDate", equipmentVo.getProductDate());
		params.put("company", equipmentVo.getCompany());
		params.put("id", equipmentVo.getId());
		String hql="update EquipmentEntity e set e.equipmentName=:equipmentName,e.equipmentNum=:equipmentNum,e.equipmentCategory=:equipmentCategory,e.equipmentType=:equipmentType"
				+ ",e.productDate=:productDate,e.company=:company where e.id=:id";
		equipmentDao.executeHql(hql, params);
	}

	
	@Override
	public List<EquipmentVo> dataGrid(EquipmentVo equipmentVo, PageFilter pageFilter){
		List<EquipmentVo> ul = new ArrayList<EquipmentVo>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from EquipmentEntity t ";
		List<EquipmentEntity> list = equipmentDao.findList(hql + whereHql(equipmentVo, params) + orderHql(pageFilter), params, pageFilter.getPage(), pageFilter.getRows());
		for (EquipmentEntity t : list) {
			EquipmentVo u = new EquipmentVo();
			BeanUtils.copyProperties(t, u);
			
			if(EquipmentCategory.XIC.getCategory()==t.getEquipmentCategory()){
				u.setCategoryName(EquipmentCategory.XIC.getName());
			}else if (EquipmentCategory.CHEC.getCategory()==t.getEquipmentCategory()) {
				u.setCategoryName(EquipmentCategory.CHEC.getName());
			}
			if(EquipmentType.SMALL.getType()==t.getEquipmentType()){
				u.setTypeName(EquipmentType.SMALL.getName());
			}else if (EquipmentType.MIDDLE.getType()==t.getEquipmentType()) {
				u.setTypeName(EquipmentType.MIDDLE.getName());
			}else if (EquipmentType.HEAVY.getType()==t.getEquipmentType()) {
				u.setTypeName(EquipmentType.HEAVY.getName());
			}
			ul.add(u);
		}
		return ul;
	}
	
	@Override
	public Long count(EquipmentVo equipment, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from EquipmentEntity t ";
		return equipmentDao.count("select count(*) " + hql + whereHql(equipment, params), params);
	}
	
	private String whereHql(EquipmentVo equipment, Map<String, Object> params) {
		String hql = "";
		if (equipment != null) {
			hql += " where 1=1 ";
			if (equipment.getEquipmentName() != null) {
				hql += " and t.name like :name";
				params.put("name", "%%" + equipment.getEquipmentName() + "%%");
			}
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	@Override
	public List<Tree> tree() {
		List<EquipmentEntity> l = null;
		List<Tree> lt = new ArrayList<Tree>();

		l = equipmentDao.findList("select distinct entity from EquipmentEntity entity");

		if ((l != null) && (l.size() > 0)) {
			for (EquipmentEntity r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				tree.setText(r.getEquipmentName());
				lt.add(tree);
			}
		}
		return lt;
	}

	@Override
//	@Transactional
	public List<EquipmentEntity> getAllEquip() {
		List<EquipmentEntity> list=equipmentDao.findList("from EquipmentEntity");
		return list;
	}
}
